﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using Utils.DatabaseUtils;
using Utils.DC.Web.UI;
using Utils;

namespace DCSLibrary
{
    public class PoolTopicUI
    {
        private const String CACHE_KEY = "PoolTopic_GetAll";
        public int Add(int categoryPageID, string TopicContent, DateTime createDate, string createIp, int userID, bool isActive, bool isTypical, ArrayList poolAnswer )
        {
            try
            {
                string poolAnswers = "";
                foreach( string str in poolAnswer )
                {
                    poolAnswers += str + ";";
                }

                if(poolAnswers.Trim() != "") poolAnswers = poolAnswers.Substring(0, poolAnswers.Length - 1);

                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Pool_add", categoryPageID, TopicContent, createDate, createIp, userID, isActive, isTypical, poolAnswers);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int Update(int PoolTopicID, int categoryPageID, string TopicContent, DateTime createDate, string createIp, int userID, bool isActive, bool isTypical, ArrayList poolAnswer )
        {
            try
            {
                string poolAnswers = "";
                foreach (string str in poolAnswer)
                {
                    poolAnswers += str + ";";
                }

                if (poolAnswers.Trim() != "") poolAnswers = poolAnswers.Substring(0, poolAnswers.Length - 1);

                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Pool_update", PoolTopicID, categoryPageID, TopicContent, createDate, createIp, userID, isActive, isTypical, poolAnswers);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY + "_" + PoolTopicID);
                    WebUtility.RemoveCache(CACHE_KEY);
                }
                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public DataSet GetAll()
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY);
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, CommandType.StoredProcedure, "Pool_getall");
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY, ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetByID(int PoolTopicID)
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY + "_" + PoolTopicID.ToString());
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, "Pool_get", PoolTopicID);
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY + "_" + PoolTopicID.ToString(), ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public int DeleteByID(int PoolTopicID)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "PoolTopic_delete", PoolTopicID);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }
                return i;
            }
            catch (Exception e)
            {
                return -1;
            }
        }

        public int AnswerPool( int poolAnswerID, int userID, DateTime answerTime, string IP )
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "Pool_Answer", poolAnswerID, userID, answerTime, IP);
                return i;
            }
            catch (Exception e)
            {
                return -1;
            }
        }
    }
}
